package StudyCode.summary.myfile;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class GetPathNameCustom {
	private static final String SEPARATOR = "/";

	public static void main(String[] args) {
		System.out.println("E:/Copy/1/2 的父目录是: " + pathName("E:/Copy/1/2"));
	}

	private static String pathName(String path) {
		String[] split = cleanPath(path).split(SEPARATOR);
		if (split.length > 0) {
			return split[split.length - 1];
		}
		return null;
	}

	private static String cleanPath(String path) {
		if (path.endsWith(SEPARATOR)) {
			path = path.replaceAll(SEPARATOR + "+$", "");
		}
		if (path.startsWith(SEPARATOR)) {
			path = path.replaceAll("^" + SEPARATOR + "+", "");
		}
		return path.replaceAll("/+", SEPARATOR);
	}
}
